package cn.vin.ws;

import org.apache.commons.lang3.RandomUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ClientEngine2 {
    static String urlStr = "ws://localhost:7110/websocket7110";
    static String loginMsg = "{\"cmd\":2001,\"openid\":\"%s\",\"reqLogin\":{\"icon\":\"http://123\",\"name\":\"ะกอ๕\",\"curRole\":[1,2,3],\"wincount\":5,\"totalcount\":0,\"pillarMin\":1,\"pillarMax\":1,\"losecount\":2,\"levelId\":1,\"score\":%d,\"roleType\":0,\"gold\":%d,\"roleType\":%d, \"version\":100}}";
//    static String loginMsg = "{\"cmd\":2014,\"openid\":\"%s\",\"reqInvitedEnter\":{\"icon\":\"http://123\",\"name\":\"ะกอ๕\",\"curRole\":[1,2,3],\"wincount\":5,\"totalcount\":0,\"pillarMin\":1,\"pillarMax\":1,\"losecount\":2,\"levelId\":1,\"score\":%d,\"roleType\":0,\"gold\":%d,\"roleType\":%d, \"version\":100,\"fight_id\":\"c5d300aa6e694d70a0dfdbec331a488b\"}}";
    public static void main(String[] args) throws Exception {
        ReentrantLock rl = new ReentrantLock();
        Condition condition = rl.newCondition();
        List<MsgWebSocketClient> list = new ArrayList<>();
        int i = 0;
        while(i < 10) {
            MsgWebSocketClient client = new MsgWebSocketClient("testb" + i,urlStr);
            client.score = RandomUtils.nextInt(50,20000);
            client.gold = Integer.MAX_VALUE + 1000;
            client.connect();
            list.add(client);
            i++;
        }
        Thread.sleep(3000);
        System.out.println("list size========" + list.size());
        list.forEach(c -> {
//            System.out.println(c.name + "发送消息");
            c.startTime = System.currentTimeMillis();
            c.send(String.format(loginMsg,c.name,c.score,c.gold,RandomUtils.nextInt(0,2)));
            /*try {
                Thread.sleep(20L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
        });
        new Thread(() -> {
            while (true) {
                Scanner scanner = new Scanner(System.in);
                scanner.nextLine();
                System.out.println("******************");
                Collections.sort(MsgWebSocketClient.waits);
                System.out.println(MsgWebSocketClient.waits);
                System.out.println("******************");
            }
        }).start();
        rl.lock();
        condition.await();
        rl.unlock();
    }
}
